import json
import util.chash
class CBlock:
    def __init__(self,version,height,timestamp,txcounts,transactions,prevBlockHash,proof,selfhash):
        self.__height = height
        self.__timestamp = timestamp
        self.__prevBlockHash = prevBlockHash
        self.__version = version
        self.__txcounts=txcounts
        self.__transactions = transactions
        self.__proof = proof
        self.__selfhash = selfhash
       
    
    @property
    def selfhash(self):
        return self.__selfhash

    @selfhash.setter
    def selfhash(self,value):
        self.__selfhash =value
    @property
    def txcounts(self):
        return self.__txcounts

    @txcounts.setter
    def txcounts(self,value):
        self.__txcounts=value

    @property
    def proof(self):
        return self.__proof

    @proof.setter
    def proof(self,value):
        self.__proof = value

    @property
    def height(self):
        return self.__height

    @height.setter
    def height(self,value):
        self.__height = value

    @property
    def timestamp(self):
        return self.__timestamp
    
    @timestamp.setter
    def timestamp(self,timevalue):
        self.__timestamp=timevalue
    
    @property
    def prevBlockHash(self):
        return self.__prevBlockHash

    @prevBlockHash.setter
    def prevBlockHash(self,value):
        self.__prevBlockHash = value
    
    @property
    def version(self):
        return self.__version

    @version.setter
    def version(self,value):
        self.__version = value
    
    @property
    def transactions(self):
        return self.__transactions

    @transactions.setter
    def transactions(self,value):
        self.__transactions = value
  
    def serializeToJsonStr(self):
      
        return  json.dumps(self, default=lambda obj: {
            'version': obj.__version,
            'timestamp': obj.__timestamp,
            'prevBlockHash': obj.__prevBlockHash,
            'height': obj.__height,
            'proof':obj.__proof,
            'selfhash':obj.__selfhash,
            'transactions': obj.__transactions,
            'txcounts':obj.__txcounts
        })
    def serializeToPyObject(self):
        return json.loads(self.serializeToJsonStr())
    
    @classmethod
    def deserializeFromPyObject(cls,pyObject):
        return cls.deserializeFromJsonStr(json.dumps(pyObject)) 
       
    @classmethod
    def deserializeFromJsonStr(cls,jsonStrobjectdata):
        d = json.loads(jsonStrobjectdata)
        return cls(d['version'],d['height'],d['timestamp'],d['txcounts'],d['transactions'],d['prevBlockHash'],d['proof'],d['selfhash'])
    
    def serializeToJsonfile(self,filename):
        with open(filename, 'w', encoding='utf-8') as f:
            #json.dump(self, f, default=lambda obj: obj.__dict__)
            json.dump(self,f, default=lambda obj: {
            'version': obj.__version,
            'timestamp': obj.__timestamp,
            'prevBlockHash': obj.__prevBlockHash,
            'height': obj.__height,
            'proof':obj.__proof,
            'selfhash':obj.__selfhash,
            'transactions': obj.__transactions,
            'txcounts':obj.__txcounts
            })

    @classmethod
    def deserializeFromJsonfile(cls,filename):
            with open(filename,encoding='utf-8') as f:
                d = json.load(f)
                return  cls(d['version'],d['height'],d['timestamp'],d['txcounts'],d['transactions'],d['prevBlockHash'],d['proof'],d['selfhash'])

        

       
          



    


